在前几天接触了heroku部署,一个国外免费的自动化部署工具,咱buddy也说它非常好,于是我就安静的沉下心来学习下这个工具,突然发现它真的很方便,只需要将代码push到heroku的git服务器上,在服务器上,git push命令会自动触发安装、配置和部署程序,连域名都会帮你创建好。
准备工作
若想使用 Heroku,程序必须托管在 Git 仓库中。如果你的程序托管在像 GitHub 或 BitBucket 这样的远程 Git 服务器上,那么在本地 clone 程序后会创建一个本地 Git 仓库,可无缝用于 Heroku。如果你的程序没有托管在 Git 仓库中,那么必须在开发电脑上创建一个仓库。
注册账户
首先需要创建一个免费的Heroku帐户。注册后,邮箱就是你的登录账号。(注意qq邮箱是不能使用的)
安装Heroku CLI
这里是mac安装heroku客户端的方式
brew install heroku/brew/heroku
登录Heroku
$ heroku login
Enter your Heroku credentials.
Email: adam@example.com
Password (typing will be hidden):
Authentication successful.
按照提示登录就可以了,如果需要输入账号密码就直接输入邮箱和你的注册密码,网上很多教程需要输入的,但是我这里是因为刚刚在浏览器登录的,所以并没有让我输入账户密码。
heroku: Press any key to open up the browser to login or q to exit:
这里可以把你的 SSH 公钥上传到 Heroku,这一点很重要,上传后才能使用 git push 命令。正常情况下,login 命令会自动创建并上传 SSH 公钥。
要检查是否已添加密钥,可以运行
heroku keys
如果没有,可以通过运行手动添加它。
heroku keys:add
有关SSH密钥的更多信息,请参阅“ 管理SSH密钥”。
准备项目
这里我使用的是halo博客部署,这里只需要将本地代码提交。
git init
git add .
git commit -m "first commit"
之后,创建一个heroku app
heroku create
Creating app... done, ⬢ radiant-thicket-94286
https://radiant-thicket-94286.herokuapp.com/ | https://git.heroku.com/radiant-thicket-94286.git
这里可以给项目重命名下
heroku apps:rename springboot-tao-blog
Renaming radiant-thicket-94286 to springboot-tao-blog... done
https://springboot-tao-blog.herokuapp.com/ | https://git.heroku.com/springboot-tao-blog.git
Git remote heroku updated
▸ Don't forget to update git remotes for all other local checkouts of the app.
上传代码
push代码会触发部署动作,这个网站由于在国外,所以时间会较长,稍微等待...
git push heroku master
枚举对象: 29140, 完成.
对象计数中: 100% (29140/29140), 完成.
使用 12 个线程进行压缩
压缩对象中: 100% (10009/10009), 完成.
写入对象中: 100% (29140/29140), 25.84 MiB | 10.00 KiB/s, 完成.
总共 29140 (差异 14974),复用 29140 (差异 14974)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Gradle app detected
remote: -----> Spring Boot detected
remote: -----> Installing JDK 1.8... done
remote: -----> Building Gradle app...
remote: -----> executing ./gradlew build -x test
remote: Downloading https://services.gradle.org/distributions/gradle-5.6.2-all.zip
remote: .....................................................................................................................................
remote: To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/5.6.2/userguide/gradle_daemon.html.
remote: Daemon will be stopped at the end of the build stopping after processing
remote: > Task :generateLombokConfig
remote: > Task :compileJava
remote: warning: unknown enum constant When.MAYBE
remote: reason: class file for javax.annotation.meta.When not found
remote: /tmp/build_c920915b6d3fa31be6200531e02e8971/src/main/java/run/halo/app/model/entity/PostTag.java:16: warning: Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type.
remote: @Data
remote: ^
remote: /tmp/build_c920915b6d3fa31be6200531e02e8971/src/main/java/run/halo/app/model/entity/PostCategory.java:16: warning: Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type.
remote: @Data
remote: ^
remote: warning: unknown enum constant When.MAYBE
remote: reason: class file for javax.annotation.meta.When not found
remote: warning: unknown enum constant When.MAYBE
remote: reason: class file for javax.annotation.meta.When not found
remote: warning: unknown enum constant When.MAYBE
remote: reason: class file for javax.annotation.meta.When not found
remote: warning: unknown enum constant When.MAYBE
remote: reason: class file for javax.annotation.meta.When not found
remote: Note: /tmp/build_c920915b6d3fa31be6200531e02e8971/src/main/java/run/halo/app/model/enums/converter/PostTypeConverter.java uses or overrides a deprecated API.
remote: Note: Recompile with -Xlint:deprecation for details.
remote: Note: /tmp/build_c920915b6d3fa31be6200531e02e8971/src/main/java/run/halo/app/utils/XmlTransferMapUtils.java uses unchecked or unsafe operations.
remote: Note: Recompile with -Xlint:unchecked for details.
remote: 7 warnings
remote:
remote: > Task :processResources
remote: > Task :classes
remote: > Task :bootJar
remote: > Task :jar SKIPPED
remote: > Task :assemble
remote: > Task :check
remote: > Task :build
remote:
remote: BUILD SUCCESSFUL in 6m 34s
remote: 4 actionable tasks: 4 executed
remote: -----> Discovering process types
remote: Procfile declares types -> (none)
remote: Default types for buildpack -> web
remote:
remote: -----> Compressing...
remote: Done: 118.3M
remote: -----> Launching...
remote: Released v3
remote: https://springboot-tao-blog.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/springboot-tao-blog.git
* [new branch] master -> master
之后就可以拿着 https://springboot-tao-blog.herokuapp.com/ 去访问网站啦。。。
参考
heroku部署项目上线 小龙123
Heroku Springboot项目部署官方文档
heroku部署springboot项目并连接数据库